package com.zy;

/**
 * @program: shopping153_cloud
 * @description:  构造器模式的Person类
 * @author: zy
 * @create: 2025-08-27 14:25
 */
public class Person {

    private String name;
    private int age;

    //构造器
    public Person(Builder builder){
        this.name = builder.name;
        this.age = builder.age;
    }

    public static Builder builder(){
        return new Builder();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //内部构建器类:  静态内部类 用来初始化外部类的成员属性值
    public static final class Builder{
        private String name;
        private int age;

        public Builder name(String name){
            this.name = name;
            return this;  //返回当前对象本身( this指的是builder对象)
        }

        public Builder age(int age){
            this.age = age;
            return this;
        }

        public Person build(){
            return new Person(   this  );
        }

    }
}
